package com.phunware.swipeToDismiss.demo;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.phunware.swipeToDismiss.R;
import com.phunware.swipeToDismiss.SwipeToDismissAdapter;
import com.phunware.swipeToDismiss.SwipeToDismissAdapter.OnItemCreateDialogListener;

public class SwipeToDismissActivity extends Activity implements
		OnItemClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.swipe_to_dismiss_demo);
		ListView view = (ListView) findViewById(R.id.swipe_to_dismiss_list);
		// view.setItemsCanFocus(true);
		ArrayList<Integer> data = new ArrayList<Integer>(50);
		for (int i = 0; i < 50; i++) {
			data.add(i);
		}

		SwipeToDismissAdapter<Integer> adapter = new SwipeToDismissAdapter<Integer>(
				data, new DataBinder());
		adapter.setOnItemCreateDialogListener(new OnItemCreateDialogListener() {

			@Override
			public Dialog onItemCreateDialog(Object item) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						SwipeToDismissActivity.this);
				builder.setTitle("Delete Item?").setMessage(
						"Are you sure you wish to delete item " + (item) + "?");
				builder.setPositiveButton("Ok", null);
				builder.setNegativeButton("Cancel", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
				return builder.create();
			}
		});
		view.setAdapter(adapter);
		view.setOnItemClickListener(this);
    }

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Integer item = (Integer) parent.getItemAtPosition(position);
		Toast.makeText(this, item + "", Toast.LENGTH_LONG).show();
	}
}